package com.edooschool.oral.arithmetic.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.utils.JsonUtils;
import com.edooschool.entity.ShareRecord;
import com.edooschool.entity.oral.OralAnswerRecord;
import com.edooschool.entity.oral.OralQuestion;
import com.edooschool.entity.oral.OralRound;
import com.edooschool.entity.oral.OralStat;
import com.edooschool.oral.arithmetic.dao.OralArithmeticDao;
import com.edooschool.oral.arithmetic.entity.OralRoundResult;
import com.edooschool.oral.arithmetic.service.OralService;

/**
 * 
 * @author mohanlan
 *
 */
@Service("oralService")
public class OralServiceImpl implements OralService {
    
    @Dao
    private OralArithmeticDao oralArithmeticDao;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /*
     * (non-Javadoc)
     * @see com.edooschool.oral.arithmetic.service.OralService#countUser()
     */
    public int countUser() {
        return genericDaoSupport.searchForInt("SELECT COUNT(1) FROM OralAnswerRecord GROUP BY openid");
    }
    
    /*
     * (non-Javadoc)
     * @see com.edooschool.oral.arithmetic.service.OralService#countRecord()
     */
    public int countRecord() {
        return genericDaoSupport.searchForInt("SELECT COUNT(1) FROM OralAnswerRecord");
    }
    
    /*
     * (non-Javadoc)
     * @see com.edooschool.oral.arithmetic.service.OralService#countShare()
     */
    public int countShare() {
        return genericDaoSupport.searchForInt("SELECT COUNT(1) FROM ShareRecord");
    }
    
    /*
     * (non-Javadoc)
     * @see com.edooschool.oral.arithmetic.service.OralService#loadOralRound(java.lang.Long)
     */
    public OralRound loadOralRound(Long id) {
        return oralArithmeticDao.loadOralRound(id);
    }
    
    /*
     * (non-Javadoc)
     * @see com.edooschool.oral.arithmetic.service.OralService#listOralRound(java.lang.Long)
     */
    public List<OralRound> listOralRound(Long groupId) {
        return oralArithmeticDao.listOralRound(groupId);
    }

    /*
     * (non-Javadoc)
     * @see com.edooschool.oral.arithmetic.service.OralService#loadOralAnswerRecord(java.lang.Long)
     */
    public OralAnswerRecord loadOralAnswerRecord(Long id) {
        return oralArithmeticDao.loadOralAnswerRecord(id);
    }

    /*
     * (non-Javadoc)
     * @see com.edooschool.oral.arithmetic.service.OralService#listQralQuestion(com.edooschool.entity.OralRound)
     */
    public List<OralQuestion> listQralQuestion(OralRound oralRound) {
        
        String questionsString = oralRound.getQuestions();
        
        String[] questionsArray = StringUtils.split(questionsString, ",");
        
        List<Long> ids = new ArrayList<Long>();
        for(String questionId : questionsArray) {
            ids.add(Long.parseLong(questionId));
        }
        
        return oralArithmeticDao.listOralQuestion(ids);
    }
    
    /*
     * (non-Javadoc)
     * @see com.edooschool.oral.arithmetic.service.OralService#saveOralAnswer(com.edooschool.entity.OralRound, com.edooschool.oral.arithmetic.entity.OralRoundResult, java.lang.String)
     */
    public OralAnswerRecord saveOralAnswer(OralRound round, OralRoundResult oralRoundResult, String openid) {
        
        OralAnswerRecord record = new OralAnswerRecord(round, 
                                                       oralRoundResult.getSuccessCount(),
                                                       oralRoundResult.getFailCount(), 
                                                       oralRoundResult.getSeconds(),
                                                       oralRoundResult.getBranchTotal(),
                                                       JsonUtils.toJson(oralRoundResult));
        
        record.bindUser(openid);
        
        genericDaoSupport.save(record.init());
        
        return record;
    }
    
    /*
     * (non-Javadoc)
     * @see com.edooschool.oral.arithmetic.service.OralService#shareOralArithmetic(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
     */
    public void shareOralArithmetic(String openid, Long roundId, String url, String marks, String type) {
        
        // step1 save share record
        ShareRecord shareRecord = new ShareRecord(openid, url, type, marks);
        genericDaoSupport.save(shareRecord.init());
        
        // step2 update oral round stat if id exit
        if(roundId != null) {
            OralStat stat = oralArithmeticDao.loadOralStat(roundId);
            genericDaoSupport.update(stat.updateShare());
        }
    }
    
    /*
     * (non-Javadoc)
     * @see com.edooschool.oral.arithmetic.service.OralService#latestOralAnswerRecord(java.lang.String)
     */
    public List<OralAnswerRecord> listOralAnswerRecord(String openid) {
        return oralArithmeticDao.latestAnswerRecord(openid);
    }
    
    /*
     * (non-Javadoc)
     * @see com.edooschool.oral.arithmetic.service.OralService#listOralAnswerRecord(java.lang.String, java.lang.Long)
     */
    public List<OralAnswerRecord> listOralAnswerRecord(String openid, Long roundId) {
        return oralArithmeticDao.listOralAnswerRecord(openid, roundId);
    }

}
